package com.lz.ht.service.serviceImpl;

import com.lz.ht.dao.RoomAdvanceMapper;
import com.lz.ht.model.Advance;
import com.lz.ht.model.Room;
import com.lz.ht.page.PageModel;
import com.lz.ht.service.RoomAdvanceService;
import com.lz.ht.util.ToolKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.apache.ibatis.exceptions.TooManyResultsException;
import java.util.List;
import java.util.HashMap;

@Service
@Transactional(rollbackFor = Exception.class)
public class RoomAdvanceServiceImpl implements RoomAdvanceService {

    @Autowired
    private RoomAdvanceMapper roomadvanceMapper;

    @Override
    public void add(Advance advance) {
        roomadvanceMapper.add(advance);
    }

    @Override
    public Room findById(Long rno) {
        return  roomadvanceMapper.findById(rno);
    }

    @Override
    public List<Room> findAll() {
        return roomadvanceMapper.findAll();
    }


    @Override
    public Room findOne(Room room) {
        List<Room>  list = roomadvanceMapper.findList(room);
        if ((list!=null)&&(list.size() == 1)) {
            return list.get(0);
        } else if ((list!=null)&&(list.size() > 1)) {
            throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
        } else {
            return null;
        }
    }

    @Override
    public List<Room> findList(Room room){
        return  roomadvanceMapper.findList(room);
    }

    @Override
    public List<Room> findPageList(PageModel<Room> page, Room room) throws Exception {
        HashMap<String, Object> hashMap = ToolKit.javaBeanToMap(room);
        List<Room> pageList = findListByMapLimit(hashMap, page.getMsFirst(), page.getMsLast());
        return pageList;
    }

    @Override
    public List<Room> findListByMapLimit(HashMap<String,Object> hashMap,long first,long last){
        hashMap.put("msFirst",first);
        hashMap.put("msLast",last);
        return roomadvanceMapper.findListByMapLimit(hashMap);
    }

    @Override
    public long findCount(Room  room) throws Exception {
        HashMap<String, Object> hashMap = ToolKit.javaBeanToMap(room);
        return  findCountByMap(hashMap);
    }

    @Override
    public long findCountByMap(HashMap<String,Object> hashMap){
        return roomadvanceMapper.findCountByMap(hashMap);
    }

    @Override
    public List<Room> findPageListByDto(PageModel<Room> page, Room room) throws Exception {
        HashMap<String, Object> hashMap = ToolKit.javaBeanToMap(room);
        hashMap.put("msFirst",page.getMsFirst());
        hashMap.put("msLast",page.getMsLast());
        return roomadvanceMapper.findPageListByDto(hashMap);
    }

    @Override
    public long findCountByDto(Room room) throws Exception {
        HashMap<String, Object> hashMap = ToolKit.javaBeanToMap(room);
        return roomadvanceMapper.findCountByDto(hashMap);
    }


}
